package com.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.InetAddress;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @ClassName DruidDemo
 * @Description druid
 * @Author 2214361854
 * @Date 2022/3/10 20:27
 * @Version 1.0
 */
public class DruidDemo {
    public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.load(new FileInputStream("src/main/resources/druid.properties"));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        Connection connection = dataSource.getConnection();

//        testSelectAll(connection);

//        Student student = new Student(4, "小强", "女");
//        int i1 = testAdd(student, connection);
//        int i2 = testUpdate(student, connection);
//        int i3 = testDelete(4, connection);

//        System.out.println(connection);
//        System.out.println(System.getProperty("user.dir"));
    }

    private static int testDelete(int id, Connection connection) throws Exception{
        String sql = "DELETE FROM Student WHERE id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,id);
        int i = preparedStatement.executeUpdate();
        return i;
    }

    private static int testUpdate(int id,Student student, Connection connection) throws Exception{
        String sql = "UPDATE Student SET name=?,sex=? WHERE id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1,student.getName());
        preparedStatement.setString(2,student.getSex());
        preparedStatement.setInt(3,student.getId());
        int i = preparedStatement.executeUpdate();
        return i;
    }

    private static int testAdd(Student student,Connection connection) throws Exception {
        String sql = "INSERT INTO Student VALUES(?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,student.getId());
        preparedStatement.setString(2,student.getName());
        preparedStatement.setString(3,student.getSex());
        int i = preparedStatement.executeUpdate();
        return i;
    }

    private static void testSelectAll(Connection connection) throws Exception {
        String sql = "SELECT * FROM student";
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        List<Student> list = new ArrayList<>();
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String name = resultSet.getString("name");
            String sex = resultSet.getString("sex");
            list.add(new Student(id,name,sex));
        }
        System.out.println(list);
    }


}
